package com.xc.content.utils;

import lombok.Data;

import java.io.Serializable;

/**
 * @Author: MangoPie
 * @CreateTime: 2024-12-26
 * @Version: 1.0
 * 封装统一返回结果
 */
@Data
public class ResponseResult<T> implements Serializable {

    private static final long serialVersionUID = 1L; // 用于序列化

    // Getter 和 Setter 方法
    private int code;    // 状态码
    private String message; // 消息
    private T data;      // 返回的数据

    // 常用的状态码
    public static final int SUCCESS_CODE = 200;
    public static final int ERROR_CODE = 500;
    public static final int UNAUTHORIZED_CODE = 401;
    public static final int FORBIDDEN_CODE = 403;

    // 构造方法
    public ResponseResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public ResponseResult() {

    }


    // 静态方法，方便创建成功结果
    public static <T> ResponseResult<T> success(T data) {
        return new ResponseResult<>(SUCCESS_CODE, "操作成功", data);
    }

    public static <T> ResponseResult<T> success(String message, T data) {
        return new ResponseResult<>(SUCCESS_CODE, message, data);
    }

    public static <T> ResponseResult<T> success() {
        return new ResponseResult<>(SUCCESS_CODE, "操作成功", null);
    }


    // 静态方法，方便创建失败结果
    public static <T> ResponseResult<T> error(String message) {
        return new ResponseResult<>(ERROR_CODE, message, null);
    }
    public static <T> ResponseResult<T> error(int code ,String message) {
        return new ResponseResult<>(code, message, null);
    }public static <T> ResponseResult<T> error(int code ,String message,T data) {
        return new ResponseResult<>(code, message, data);
    }

    public static <T> ResponseResult<T> error() {
        return new ResponseResult<>(ERROR_CODE, "操作失败", null);
    }

    public static <T> ResponseResult<T> error(int code) {
        return new ResponseResult<>(code, "操作失败", null);
    }

    public static <T> ResponseResult<T> unauthorized(String message) {
        return new ResponseResult<>(UNAUTHORIZED_CODE, message, null);
    }

    public static <T> ResponseResult<T> forbidden(String message) {
        return new ResponseResult<>(FORBIDDEN_CODE, message, null);
    }
}
